using System;
using System.Collections.Specialized;
using System.Web.Security;
using Castle.MonoRail.Framework;
using Castle.MonoRail.Framework.Services;
using WineCellar.Web.Common;

namespace WineCellar.Web.Controllers
{
    [Layout("index")]
    public class UserRegisterController:MessageControllerBase<UserRegisterController.PropertyAccessorImpl>
    {
        public class PropertyAccessorImpl:PropertyAccessorBase 
        {
            public class Fields
            {
                public const string RequiresApproval = "requiresApproval";
                public const string Succes = "succes";
                public const string FailureMessage = "failureMessage";
            }

            public bool RequiresApproval
            {
                get { return (bool)this.PropertyBag[Fields.RequiresApproval]; }
                set { this.PropertyBag[Fields.RequiresApproval] = value; }
            }
            public bool Success
            {
                get { return (bool) PropertyBag[Fields.Succes]; }
                set { PropertyBag[Fields.Succes] = value; }
            }
            public string FailureMessage
            {
                get { return (string)PropertyBag[Fields.FailureMessage]; }
                set { PropertyBag[Fields.FailureMessage] = value; }
            }
        }
        public class UserApprovalEmailPropertyAccessor:MembershipMailPropertyAccessor
        {
            protected new class Fields:MembershipMailPropertyAccessor.Fields
            {
			    public const string ConfirmUrl= "confirmUrl";
            }
            public UserApprovalEmailPropertyAccessor(Controller controller, MembershipUser user):
                base(controller,user)
            {

            }
            public string ConfirmUrl
            {
                get { return (string) this.Controller.PropertyBag[Fields.ConfirmUrl]; }
                set { this.Controller.PropertyBag[Fields.ConfirmUrl] = value; }
            }

        }
        public void Index()
        {
            CancelLayout();
        }
        public void CreateUser(string userName, string password, string email, string passwordVerify, string passwordQuestion, string answer)
        {
            DefaultUrlBuilder _urlBuilder = new DefaultUrlBuilder();
			UrlBuilderParameters parameters = new UrlBuilderParameters(Context.UrlInfo.Controller,"Confirm");
            //TODO: Render message. Ask user to check for confirmation email if RequiresApproval is enabled
            MembershipManagerSection membershipManagerSection = MembershipManagerSection.GetConfig();

            MembershipCreateStatus resultStatus;
            MembershipUser user = Membership.CreateUser(userName, password, email, passwordQuestion, answer, !membershipManagerSection.RequiresApproval, out resultStatus);
            if (membershipManagerSection.RequiresApproval)
            {
                UserApprovalEmailPropertyAccessor userApprovalEmailPropertyAccessor = new UserApprovalEmailPropertyAccessor(this, user);
                OrderedDictionary urlParts = UrlHelper.ParseUrl(Request.Uri.ToString());
                userApprovalEmailPropertyAccessor.ConfirmUrl = string.Format("{0}://{1}{2}", urlParts["protocol"], urlParts["hostname"], _urlBuilder.BuildUrl(Context.UrlInfo,parameters) + string.Format("?providerUserKey={0}", user.ProviderUserKey.ToString()));

                RenderEmailAndSend("UserApproval");
            }
            PropertyAccessor.RequiresApproval = membershipManagerSection.RequiresApproval;
            if (resultStatus == MembershipCreateStatus.Success)
            {
                PropertyAccessor.Success = true;                     
            }
            else
            {
                PropertyAccessor.Success = false;
                PropertyAccessor.FailureMessage = resultStatus.ToString();                
            }
        }
        public void Confirm(Guid providerUserKey)
        {
            MembershipUser user = Membership.GetUser(providerUserKey,true);
            user.IsApproved = true;
            Membership.UpdateUser(user);
        }
    }
}
